<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>员工管理</title>
    <#include "/common/link.ftl">

</head>
<body class="hold-transition skin-black sidebar-mini">
<div class="wrapper">
    <#include "/common/navbar.ftl">
    <!--菜单回显-->
    <#assign currentMenu="employee"/>
    <#include "/common/menu.ftl">
    <div class="content-wrapper">
        <section class="content-header">
            <h1>员工管理</h1>
        </section>
        <section class="content">
            <div class="box">
                <!--高级查询--->
                <div style="margin: 10px;">
                    <form class="form-inline" id="searchForm" action="/emp/list" method="post">
                        <input type="hidden" name="currentPage" id="currentPage" value="1">
                        <div class="form-group">
                            <label for="keyword">关键字:</label>
                            <input type="text" class="form-control" name="keyword"
                                   value="${qo.keyword}" placeholder="请输入姓名/邮箱">
                        </div>
                        <div class="form-group">
                            <label for="dept"> 部门:</label>
                            <select class="form-control" id="dept" name="deptId">
                                <option value="">全部</option>
                                <#--回显选中下拉框的操作-->
                                <#list departments as department>
                                    <option value="${department.id}">${department.name}</option>
                                </#list>
                                <script>
                                    <#--点击查询，提交表单，将下拉框中<option>标签的value值给到select中的name对应的deptId,
                                    然后传到员工的controller中，封装到过滤查询的参数qo中，在ftl文件中使用${qo.deptId}取出deptId给到
                                    select中的option中的value，然后<option value="${qo.deptId}">${department.name}</option>回显对应的部门-->
                                    $('#dept').val(${qo.deptId});
                                </script>
                            </select>
                        </div>
                        <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-search"></span>
                            查询
                        </button>
                        <a href="/emp/input" class="btn btn-success btn-input">
                            <span class="glyphicon glyphicon-plus"></span> 添加
                        </a>
                        <a href="/emp/exportXls" class="btn btn-warning">
                            <span class="glyphicon glyphicon-download"></span> 导出
                        </a>

                        <a href="#" class="btn btn-warning btn-import">
                            <span class="glyphicon glyphicon-upload"></span> 导入
                        </a>
                        <a href="#" class="btn btn-warning btn-batchDelete">
                            <span class="glyphicon glyphicon-upload"></span>批量删除
                        </a>
                    </form>
                </div>
                <div class="box-body table-responsive ">
                    <table class="table table-hover table-bordered table-striped">
                        <thead>
                        <tr>
                            <th><input type="checkbox" id="allCb"></th>
                            <th>序号</th>
                            <th>用户名</th>
                            <th>真实姓名</th>
                            <th>邮箱</th>
                            <th>年龄</th>
                            <th>权限</th>
                            <th>部门</th>
                            <th>用户状态</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>

                        <#list pageInfo.list as employee>
                            <tr>
                                <td><input type="checkbox" class="cb" value="${employee.id}"></td>
                                <td>${employee_index+1}</td>
                                <td>${employee.username}</td>
                                <td>${employee.name}</td>
                                <td>${employee.email}</td>
                                <td>${employee.age}</td>
                                <td>${employee.admin ? string('是','否')}</td>
                                <td>${employee.dept.name}</td>
                                <td>${(employee.status?string('正常','禁用'))!}</td>
                                <td>
                                    <a href="/emp/input?id=${employee.id}" class="btn btn-info btn-xs btn_redirect">
                                        <span class="glyphicon glyphicon-pencil"></span> 编辑
                                    </a>
                                    <a class="btn btn-danger btn-xs btn-delete"
                                       data-url="/emp/delete?id=${employee.id}">
                                        <span class="glyphicon glyphicon-trash"></span> 删除
                                    </a>
                                    <#if employee.status>
                                        <a href="/emp/setStatus?id=${employee.id}"
                                           class="btn btn-info btn-xs btn_status">
                                            <span class="glyphicon glyphicon-pencil"></span>禁用
                                        </a>
                                    </#if>
                                    <#if !employee.status>
                                        <a href="/emp/setStatus?id=${employee.id}"
                                           class="btn btn-info btn-xs btn_status" >
                                            <span class="glyphicon glyphicon-pencil"></span>恢复
                                        </a>
                                    </#if>
                                </td>
                            </tr>
                        </#list>
                        </tbody>
                    </table>
                    <!--分页-->
                    <#include "/common/page.ftl">
                </div>

            </div>
        </section>
    </div>
    <#include "/common/footer.ftl">
</div>
<script>
    /*var idArray =[];//定义一个数组
     $.each($('input:checkbox:checked'),function(){
     idArray.push($(this).val()); //塞值
     });*/
    $(".btn-batchDelete").click(function () {
        var ids = [];
        //获取所有的复选框值
        $.each($('.cb:checked'), function () {
            ids.push($(this).val());
        })
        // $('.cb:checked').each(function (index,data) {
        //     ids.push($(data).val());
        // })
        jQuery.ajaxSettings.traditional = true;

        $.ajax({
            url: "/emp/batchDelete",
            type: "post",
            data: {'ids': ids},
            success: function (data) {
                window.location.reload();
            },
            error: function () {
                alert("删除失败");
            }
        })
    })

    //点击禁用按钮,发送ajax请求，用户id传递过去，然后隐藏（或者值改变为恢复）
    $(".btn-status").click(function () {
        $(".btn-status").val('恢复');

        $.post('/emp/setStatus', function () {
            //回调函数方法处理返回的结果，这里暂时不要就行

        })
    })
</script>

</body>
</html>
